package com.web.dao.impl;

import com.util.LogUtil;
import com.web.dao.RoomDao;
import com.web.domain.Room;
import com.web.vo.RoomVo;
import com.web.vo.mapper.RoomVoMapper;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈国钰 on 2016/9/5.
 */
@Repository
public class RoomDaoImpl implements RoomDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Resource
    SessionFactory factory;

    public List<RoomVo> searchRooms(int hotelId) {
        List<Object> params = new ArrayList<Object>();
        String sql =
                "SELECT\n" +
                        "	*, count(roomType) AS count\n" +
                        "FROM\n" +
                        "	t_room\n" +
                        "WHERE\n" +
                        "	roomStatus = 0\n" +
                        "AND hotelId = ?\n" +
                        "GROUP BY\n" +
                        "	roomType";
        LogUtil.sqlLog("获得所有类型的房间以及剩余数目",sql);
        params.add(hotelId);
        return jdbcTemplate.query(sql, params.toArray(),new RoomVoMapper());
    }


    public Room searchRoomById(int roomId) {
        return (Room) factory.getCurrentSession().get(Room.class, roomId);
    }


    public void modify(Room room) {
        factory.getCurrentSession().update(room);
    }

}
